<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        html,
        body {
            margin: 0;
            padding: 0;
        }

        html {
            /* font-size: 10px; */
        }

        #box {
            width: 5rem;
            height: 5rem;
            background-color: red;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <script>
        const docEl = document.documentElement

        function setRem() {
            let rem = docEl.clientWidth / 10
            docEl.style.fontSize = rem + 'px'

        }
        // 监听屏幕窗口的变化
        window.addEventListener('resize', function () {
            setRem()
        })
        // 监听页面初次打开
        window.addEventListener('pageshow', function () {
            console.log('pageshow');
        })
    </script>

</body>

</html>